<?php
namespace app\admin\controller;
use think\Controller;

class System extends Base
{
	/**
	 * 车源全局参数列表
	 */
	public function carconfig(){
		//是否展示
		if($this->request->isPost() && isset($_POST['lock'])){
			//dump(input());
			if(input('post.lock')==1){
				model("Carconfig")->where('cid='.input('post.id'))->update(['is_show'=>0]);
			}else{
				model("Carconfig")->where('cid='.input('post.id'))->update(['is_show'=>1]);
			};
			return $this->success('1');
		}
		//排序
		if(input("sort") !== null){
			model("Carconfig")->where("cid",input("id"))->update(['sort'=>input("sort")]);
		}
		//搜索功能
		$condition=[];
		if(input('name')){
			$condition[]=['name','like',"%".input('name')."%"];
		}
		$info = model("Carconfig")
			->where($condition)
			->order(['sort'=>'desc'])
			->paginate([
				'list_rows'=>10,
				'query'=>input('param.'),
			]);
		$this->assign('info',$info);
		return $this->fetch();
	}

	/**
	 * 车源全局参数--添加
	 */
	public function carconfigadd(){
		if($this->request->isPost()){
			$re = model("Carconfig")->insert([
				'name'=>input('post.name'),
				'val'=>input('post.val'),
				'type'=>input('post.type'),
				'sign'=>input('post.sign'),
			]);
			if($re > 0){
				return $this->success('添加成功');
			}else{
				return $this->error('添加失败');
			}
		}else{
			return $this->fetch();
		}
	}

	/**
	 * 车源全局参数--修改
	 */
	public function carconfigedit(){
		if($this->request->isPost()){
			$re = model("Carconfig")->where('cid',input('cid'))->update([
				'name'=>input('post.name'),
				'val'=>input('post.val'),
				'type'=>input('post.type'),
				'sign'=>input('post.sign'),
			]);
			if($re > 0){
				return $this->success('修改成功');
			}else{
				return $this->error('未作任何修改');
			}
		}else{
			$info = model("Carconfig")->where('cid',input('cid'))->select();
			$this->assign('info',$info);
			return $this->fetch();
		}
	}

	/**
	 * 车源全局参数--删除
	 */
	public function carconfigdel(){
		$id=input('post.id');
		if(!empty(input('post.allid'))){
    		$id=substr(input('post.allid'),0,-1);
    	}
    	$re=model('Carconfig')->where("cid","in",$id)->delete();
    	if($re > 0){
    		return $this->success('删除成功');
    	}else{
    		return $this->error('删除失败');
    	}
	}
}